{% extends '../_manage.html' %}

{% block title %} {{ ('All Ad Materials') }} {% endblock %}

{% block head %}
<script>

$(function() {
    getJSON('/api/adperiods', function (err, data) {
        if (err) {
            return showError(err);
        }
        var adperiods = data.adperiods;
        var findAdPeriodById = function (id) {
            var i;
            for (i=0; i<adperiods.length; i++) {
                if (adperiods[i].id === id) {
                    return adperiods[i];
                }
            }
            throw 'Logic error: cannot find adperiod id: ' + id;
        };
        getJSON('/api/admaterials', function (err, data) {
            if (err) {
                return showError(err);
            }
            var
                i,
                admaterial,
                admaterials = data.admaterials;
            for (i=0; i<admaterials.length; i++) {
                admaterial = admaterials[i];
                admaterial.adperiod = findAdPeriodById(admaterial.adperiod_id);
            }
            initVM(data.admaterials);
        });
    });
});

function initVM(admaterials) {
    var vm = new Vue({
        el: '#vm',
        data: {
            admaterials: admaterials
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            find: function (id) {
                for (var i = 0; i < this.admaterials.length; i ++) {
                    var p = this.admaterials[i];
                    if (p.id===id) {
                        return i;
                    }
                }
                throw 'logic error';
            },
            deleteAdMaterial: function (id) {
                var p = this.admaterials[this.find(id)];
                var that = this;
                UIkit.modal.confirm('Ad material will be deleted. Continue?', function() {
                    that.$resource('/api/admaterials/' + p.id + '/delete').save({}).then(function(resp) {
                        resp.json().then(function (result) {
                            refresh();
                        });
                    }, onJsonError);
                });
            }
        }
    });
};

</script>
{% endblock %}

{% block main %}

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-tab" data-uk-tab>
            <li><a href="#0" onclick="location.assign('adslot_list')">{{ _('Ad Slots') }}</a></li>
            <li><a href="#0" onclick="location.assign('adperiod_list')">{{ _('Ad Periods') }}</a></li>
            <li class="uk-active"><a href="#0">{{ _('Ad Materials') }}</a></li>
        </ul>

        <div class="uk-margin">
            <a href="javascript:refresh()" class="uk-button"><i class="uk-icon-refresh"></i> {{ _('Refresh') }}</a>
        </div>

        <table class="uk-table uk-table-hover">
            <thead>
                <tr>
                    <th width="10%">{{ _('Image') }}</th>
                    <th width="10%">{{ _('User') }}</th>
                    <th width="10%">{{ _('Start At') }}</th>
                    <th width="10%">{{ _('End At') }}</th>
                    <th width="5%">{{ _('Weight') }}</th>
                    <th width="10%">{{ _('GEO') }}</th>
                    <th width="10%">{{ _('Keywords') }}</th>
                    <th width="30%">{{ _('URL') }}</th>
                    <th width="5%">{{ _('Action') }}</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="m in admaterials">
                    <td>
                        <a v-bind:href="'/files/attachments/' + m.cover_id + '/l'" target="_blank"><img v-bind:src="'/files/attachments/' + m.cover_id + '/s'" style="width: 160px;"></a>
                    </td>
                    <td>
                        <a v-bind:href="'/user/' + m.adperiod.user_id" v-text="m.adperiod.user.name" target="_blank"></a>
                    </td>
                    <td v-text="m.start_at"></td>
                    <td v-text="m.end_at"></td>
                    <td v-text="m.weight"></td>
                    <td v-text="m.geo"></td>
                    <td v-text="m.keywords"></td>
                    <td>
                        <a v-bind:href="m.url" v-text="m.url" target="_blank"></a>
                    </td>
                    <td>
                        <a v-on:click="deleteAdMaterial(m.id)" title="Delete Ad Material" href="#0" class="x-btn"><i class="uk-icon-trash"></i></a>
                    </td>
                </tr>
            </tbody>
        </table>
        <div v-if="admaterials.length===0" class="x-empty-list">
            <p>No ad material found.</p>
        </div>
    </div>

{% endblock%}
